package com.itheima.exception;

import com.itheima.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler
    public Result  handleExceptionHandler(Exception e){
        log.error("程序出错了~",e);
        return Result.error("出错了，请练习管理员~");

    }
    @ExceptionHandler
    public Result handleDuplicateKeyException(DuplicateKeyException e){
        log.error("程序出错了~",e);
        String message=e.getMessage();
        int i=message.indexOf("Duplicate entry");
        String errMsg = message.substring(i);
        String[] arr = errMsg.split(" ");
        return Result.error(arr[2]+"已存在");

    }
    @ExceptionHandler
    public Result handlerCustomException(CustomException e){
        log.error("关联其他表~",e);
        return Result.error("班级下有学员，不能直接删除");
    }

    @ExceptionHandler
    public Result handlerdeptException(deptException e){
        log.error("关联其他表~",e);
        return Result.error("部门下有员工，不能直接删除");
    }
}
